package cn.wolfcode.rbac.util;

import cn.wolfcode.rbac.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public abstract class UserContext {
    public static final String EMP_IN_SESSION="EMP_IN_SESSION";
    public static final String EXPRESSION_IN_SESSION="EXPRESSION_IN_SESSION";

    //将用户信息存入session
    public static void setCurrentUser(Employee emp){
        getSession().setAttribute("EMP_IN_SESSION",emp);
    }

    //获取session中的用户信息
    public static Employee getCurrentUser(){
        return (Employee)getSession().getAttribute("EMP_IN_SESSION");
    }

    //获取session
    public static HttpSession getSession(){
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attrs.getRequest().getSession();
    }

    //将权限表达式保存到session
    public static void setExpression(List<String> permissions){
        getSession().setAttribute("EXPRESSION_IN_SESSION",permissions);
    }

    //获取去session中的权限表达式
    public static List<String> getExpression(){
        return (List)getSession().getAttribute("EXPRESSION_IN_SESSION");
    }
}
